@echo off
chcp 65001
setlocal enabledelayedexpansion

echo ============================================
echo      剪切板翻译器可执行JAR构建工具
echo ============================================

REM 检查Java环境
echo 正在检查Java环境...
java -version >nul 2>&1
if %ERRORLEVEL% NEQ 0 (
  echo 错误: 未找到Java。请确保已安装JDK并设置了JAVA_HOME环境变量。
  exit /b 1
)

REM 设置目录
set SRC_DIR=src
set BUILD_DIR=build\classes
set DIST_DIR=dist
set LIB_DIR=lib
set JAR_NAME=ctcat.jar
set MANIFEST=build\MANIFEST.MF

REM 创建目录结构
echo 创建目录结构...
if not exist build mkdir build
if not exist %BUILD_DIR% mkdir %BUILD_DIR%
if not exist %DIST_DIR% mkdir %DIST_DIR%
if not exist %DIST_DIR%\lib mkdir %DIST_DIR%\lib

REM 构建类路径
echo 构建类路径...
set CLASSPATH=.
for %%i in (%LIB_DIR%\*.jar) do (
  set CLASSPATH=!CLASSPATH!;%%i
)

REM 编译Java代码
echo 编译Java代码...
echo 使用类路径: %CLASSPATH%
echo 查找主类文件...
dir /b /s %SRC_DIR%\com\inputabc\ct\v1\app\*.java

REM 编译所有Java文件而不是指定特定文件
echo 编译所有Java文件...
javac -encoding UTF-8 -d %BUILD_DIR% -cp %CLASSPATH% %SRC_DIR%\com\inputabc\ct\v1\app\App.java -sourcepath %SRC_DIR%

if %ERRORLEVEL% NEQ 0 (
  echo 编译失败！
  exit /b 1
)

REM 复制资源文件
echo 复制资源文件...
xcopy /E /Y /I %SRC_DIR%\icon %BUILD_DIR%\icon
if exist %SRC_DIR%\META-INF xcopy /E /Y /I %SRC_DIR%\META-INF %BUILD_DIR%\META-INF

REM 创建清单文件
echo 创建清单文件...
echo Manifest-Version: 1.0 > %MANIFEST%
echo Main-Class: com.inputabc.ct.v1.app.App >> %MANIFEST%
echo Class-Path: . >> %MANIFEST%

REM 添加所有JAR到类路径
echo  lib/asm-3.3.1.jar lib/cglib-2.2.2.jar lib/commons-beanutils-1.9.3.jar >> %MANIFEST%
echo   lib/commons-codec-1.11.jar lib/commons-collections-3.2.jar >> %MANIFEST%
echo   lib/commons-io-2.6.jar lib/commons-lang-2.3.jar >> %MANIFEST%
echo   lib/commons-lang3-3.6.jar lib/commons-logging-1.2.jar >> %MANIFEST%
echo   lib/EzGUI-1.8-debug4.jar lib/ezmorph-1.0.6.jar lib/flatlaf-3.2.1.jar >> %MANIFEST%
echo   lib/fluent-hc-4.5.10.jar lib/httpclient-4.5.10.jar >> %MANIFEST%
echo   lib/httpclient-cache-4.5.10.jar lib/httpclient-osgi-4.5.10.jar >> %MANIFEST%
echo   lib/httpclient-win-4.5.10.jar lib/httpcore-4.4.12.jar >> %MANIFEST%
echo   lib/httpmime-4.5.10.jar lib/jlayer-1.0.1.jar lib/jna-4.5.2.jar >> %MANIFEST%
echo   lib/jna-platform-4.5.2.jar lib/json-lib-2.4-jdk15.jar >> %MANIFEST%
echo   lib/log4j-1.2.16.jar lib/lucene-analyzers-common-6.6.0.jar >> %MANIFEST%
echo   lib/lucene-core-6.6.0.jar lib/lucene-queryparser-6.6.0.jar >> %MANIFEST%
echo   lib/mp3spi1.9.5.jar lib/slf4j-api-1.6.1.jar lib/slf4j-log4j12-1.7.2.jar >> %MANIFEST%
echo   lib/voicerss_tts.jar lib/YoudaoBase_v2.0.1.jar >> %MANIFEST%
echo   lib/YoudaoTranslateOnline_v2.0.0.jar >> %MANIFEST%

REM 创建JAR文件
echo 创建JAR文件...
jar cfm %DIST_DIR%\%JAR_NAME% %MANIFEST% -C %BUILD_DIR% .

if %ERRORLEVEL% NEQ 0 (
  echo JAR文件创建失败！
  exit /b 1
)

REM 复制依赖库
echo 复制依赖库...
xcopy /E /Y /I %LIB_DIR% %DIST_DIR%\lib

REM 创建启动脚本
echo 创建启动脚本...
echo @echo off > %DIST_DIR%\start.bat
echo echo 正在启动剪切板翻译器... >> %DIST_DIR%\start.bat
echo java -Dfile.encoding=UTF-8 -Xmx512m -jar %JAR_NAME% >> %DIST_DIR%\start.bat
echo if %%errorlevel%% neq 0 ( >> %DIST_DIR%\start.bat
echo   echo 程序启动失败！错误代码：%%errorlevel%% >> %DIST_DIR%\start.bat
echo   pause >> %DIST_DIR%\start.bat
echo ) >> %DIST_DIR%\start.bat

REM 复制其他文件
echo 复制其他文件...
if exist README.md copy README.md %DIST_DIR%
if exist 使用说明.txt copy 使用说明.txt %DIST_DIR%
if exist 关于.txt copy 关于.txt %DIST_DIR%
if exist translation_history.txt copy translation_history.txt %DIST_DIR%
if exist settings.conf copy settings.conf %DIST_DIR%

echo.
echo 构建完成！
echo 可执行程序位于：%DIST_DIR%
echo 运行方式：双击%DIST_DIR%\start.bat
echo.
echo 构建时间: %DATE% %TIME%

pause 